#include "drawcurve.h"
#include "mainwindow.h"
#include <QDebug>

const int INCREMENT = 3;    // 温度每升高/降低1°，y轴坐标的增量
const int POINT_RADIUS = 3; // 曲线描点的大小
const int TEXT_OFFSET_X = 24;
const int TEXT_OFFSET_Y = 12;

DrawCurve::DrawCurve(QWidget* parent)
    : QWidget{ parent }, ui(MainWindow::mywidget->ui)
{
    //*安装filter
    ui->lblHighCurve->installEventFilter(this);
    ui->lblLowCurve->installEventFilter(this);

    //*初始化标签列表
    myQualitylist << ui->lblQuality0 << ui->lblQuality1 << ui->lblQuality2 << ui->lblQuality3
        << ui->lblQuality4 << ui->lblQuality5;

}
bool DrawCurve::eventFilter(QObject* watched, QEvent* event)
{
    if (watched == ui->lblHighCurve && event->type() == QEvent::Paint)
    {
        paintHighCurve();
    }
    if (watched == ui->lblLowCurve && event->type() == QEvent::Paint)
    {
        paintLowCurve();
    }

    return QWidget::eventFilter(watched, event);
}

void DrawCurve::updateData(const Today& today, const Day* days)
{
    this->mToday = today;
    for (int i = 0; i < 6; i++)
    {
        this->mDay[i] = days[i];
    }
    ui->lblLowCurve->update();
    ui->lblHighCurve->update();
}

void DrawCurve::paintHighCurve()
{
    QPainter painter(ui->lblHighCurve);

    // 抗锯齿
    painter.setRenderHint(QPainter::Antialiasing, true);
    // 1．获取x坐标
    int pointX[6] = { 0 };
    for (int i = 0; i < 6; i++)
    {
        Q_ASSERT(myQualitylist.size() >= 6);
        pointX[i] = myQualitylist[i]->pos().x() + myQualitylist[i]->width() / 2;
    }
    // 2．获取y坐标
    int tempSum = 0;
    int tempAverage = 0;
    for (int i = 0; i < 6; i++)
    {
        tempSum += mDay[i].high;
    }
    tempAverage = tempSum / 6; // 最高温的平均值

    // 计算у轴坐标
    int pointY[6] = { 0 };
    int yCenter = ui->lblHighCurve->height() / 2;
    for (int i = 0; i < 6; i++)
    {
        pointY[i] = yCenter - ((mDay[i].high - tempAverage) * INCREMENT);
    }

    //  3.开始绘制
    // 3.1初始化画笔相关
    QPen pen = painter.pen();
    pen.setWidth(1);
    // 设置画笔的宽度
    pen.setColor(QColor(255, 170, 0)); // 设置画笔的颜色
    painter.setPen(pen);
    painter.setBrush(QColor(255, 170, 0)); // 设设置画刷的颜色-内部填充的颜色
    // 3.2画点、写文本
    for (int i = 0; i < 6; i++)
    {
        painter.drawEllipse(QPoint(pointX[i], pointY[i]), POINT_RADIUS, POINT_RADIUS);

        // 显示温度文本
        painter.drawText(pointX[i] - TEXT_OFFSET_X, pointY[i] - TEXT_OFFSET_Y, QString::number(mDay[i].high) + "°C");
    }

    // 3.3 绘制曲线
    for (int i = 0; i < 5; i++)
    {
        if (i == 0)
        {
            pen.setStyle(Qt::DotLine); // 虚线
            painter.setPen(pen);
        }
        else
        {
            pen.setStyle(Qt::SolidLine); // 实线
            painter.setPen(pen);
        }
        painter.drawLine(pointX[i], pointY[i], pointX[i + 1], pointY[i + 1]);
    }
}

void DrawCurve::paintLowCurve()
{
    QPainter painter(ui->lblLowCurve);

    // 抗锯齿
    painter.setRenderHint(QPainter::Antialiasing, true);
    // 1．获取x坐标
    int pointX[6] = { 0 };
    for (int i = 0; i < 6; i++)
    {
        Q_ASSERT(myQualitylist.size() >= 6);
        pointX[i] = myQualitylist[i]->pos().x() + myQualitylist[i]->width() / 2;
    }
    // 2．获取y坐标
    int tempSum = 0;
    int tempAverage = 0;
    for (int i = 0; i < 6; i++)
    {
        tempSum += mDay[i].low;
    }
    tempAverage = tempSum / 6; // 最高温的平均值

    // 计算у轴坐标
    int pointY[6] = { 0 };
    int yCenter = ui->lblLowCurve->height() / 2;
    for (int i = 0; i < 6; i++)
    {
        pointY[i] = yCenter - ((mDay[i].low - tempAverage) * INCREMENT);
    }

    //  3.开始绘制
    // 3.1初始化画笔相关
    QPen pen = painter.pen();
    pen.setWidth(1);

    // 设置画笔的宽度
    pen.setColor(QColor(222, 99, 132)); // 设置画笔的颜色
    painter.setPen(pen);
    painter.setBrush(QColor(222, 99, 132)); // 设设置画刷的颜色-内部填充的颜色

    // 3.2画点、写文本
    for (int i = 0; i < 6; i++)
    {
        painter.drawEllipse(QPoint(pointX[i], pointY[i]), POINT_RADIUS, POINT_RADIUS);

        // 显示温度文本
        painter.drawText(pointX[i] - TEXT_OFFSET_X, pointY[i] + 2 * TEXT_OFFSET_Y, QString::number(mDay[i].low) + "°C");
    }

    // 3.3 绘制曲线
    for (int i = 0; i < 5; i++)
    {
        if (i == 0)
        {
            pen.setStyle(Qt::DotLine); // 虚线
            painter.setPen(pen);
        }
        else
        {
            pen.setStyle(Qt::SolidLine); // 实线
            painter.setPen(pen);
        }
        painter.drawLine(pointX[i], pointY[i], pointX[i + 1], pointY[i + 1]);
    }

}

